package com.ceshi.network

import android.annotation.SuppressLint
import com.books.BuildConfig
import com.books.network.ApiService


import http.QHttpInterceptor

import okhttp3.OkHttpClient

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

import java.security.*
import java.security.cert.X509Certificate
import java.util.concurrent.TimeUnit
import javax.net.ssl.*
/**
 * "ht" 创建 2022/9/1.
 * 界面名称以及功能:网络请求Retrofit2+Coroutine(协程)
 */
object Api {
    private const val DEFAULT_TIMEOUT: Long = 30
    val api: ApiService by lazy {
        //支持RxJava2
        val retrofit = Retrofit.Builder().baseUrl("https://api.book.bbdaxia.com")
//            .addConverterFactory(ScalarsConverterFactory.create()) //添加ScalarsConverterFactory支持
            .addConverterFactory(GsonConverterFactory.create())//可以接收自定义的Gson，当然也可以不传
          .client(unsafeOkHttpClient)

            .build()
        return@lazy retrofit.create(ApiService::class.java)
    }

    // Install the all-trusting trust manager TLS
    private val unsafeOkHttpClient: OkHttpClient
        get() = try {
            // Install the all-trusting trust manager TLS
            val sslContext = SSLContext.getInstance("TLS")
            sslContext.init(null, trustAllCerts, SecureRandom())
            // Create an ssl socket factory with our all-trusting manager
            val sslSocketFactory = sslContext.socketFactory
            val okBuilder = OkHttpClient.Builder()
            okBuilder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
            okBuilder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            okBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            okBuilder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            okBuilder.addInterceptor(QHttpInterceptor())
         //   okBuilder.addInterceptor(if (BuildConfig.DEBUG) HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY) else HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.NONE))
            /*okBuilder.addInterceptor { chain ->
                val request: Request = chain.request().newBuilder()
                                       .header("Cache-Control", "public") //如果只有一个请求头，就使用这
                                       .addHeader("header2","aa") //如果是多个请求头，就是用addHeader
                                       .removeHeader("Pragma")//删除掉请求过程中的所有key为Pragma的请求头
                                       .build()
                chain.proceed(request);
            }*/
            okBuilder.hostnameVerifier { _, _ -> true }
            okBuilder.build()
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    @SuppressLint("CustomX509TrustManager")
    private val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
        @SuppressLint("TrustAllX509TrustManager")
        override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
        }

        @SuppressLint("TrustAllX509TrustManager")
        override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
        }

        override fun getAcceptedIssuers(): Array<X509Certificate> {
            return arrayOf()
        }
    })

}




